package com.whale.nangua.pumpkingobang.aty;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.wuziqi.R;
import com.whale.nangua.pumpkingobang.Network.TCP_Network;
import com.whale.nangua.pumpkingobang.bean.ReplayData;
import com.whale.nangua.pumpkingobang.view.FuPanGobang;

public class FuPanAty extends Activity {

    private FuPanGobang gbv;
    Button Previous;
    Button Next;
    private ReplayData replayData;
    private int current=-1;

    //按钮监听器
    MyButtonListener myButtonListener;




    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fupan_layout);
        Intent intent= getIntent();
        Bundle data =intent.getExtras();
        replayData = (ReplayData) data.getSerializable("replay");
        initView();

    }

    private void initView() {
        myButtonListener = new MyButtonListener();
        gbv = (FuPanGobang)findViewById(R.id.fupang_gobangview);
        Button previous_btn = (Button) findViewById(R.id.previous_btn);
        Button next_btn = (Button) findViewById(R.id.next_btn);
        previous_btn.setOnClickListener(myButtonListener);
        next_btn.setOnClickListener(myButtonListener);

    }

    class MyButtonListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            int id = v.getId();//如果是上一步
            if (id == R.id.previous_btn) {
                if (current == -1) {
                    Toast.makeText(getApplicationContext(), "刚开局！", Toast.LENGTH_SHORT).show();
                } else {
                    ReplayData.MoveInfo moveInfo =replayData.getMoveInfos().get(current);

                    int x = moveInfo.getxCoordinate();
                    int y = moveInfo.getyCoordinate();
                    gbv.mGridArray[x][y] = 0;
                    gbv.invalidate();
                    current--;
                }
                //如果是下一步
            } else if (id == R.id.next_btn) {
                if (current >= replayData.getMoveInfos().size()-1) {
                    Toast.makeText(getApplicationContext(), "已结束！", Toast.LENGTH_SHORT).show();
                } else {
                    current++;
                    ReplayData.MoveInfo moveInfo =replayData.getMoveInfos().get(current);
                    int x = moveInfo.getxCoordinate();
                    int y = moveInfo.getyCoordinate();
                    int color = moveInfo.getPlayerColor();
                    gbv.putChess(x, y, color);
                    gbv.invalidate();
                }

            }
        }
    }

}
